import logging
import os
import time


class Log(logging.Logger):

    def __new__(cls, *args, **kwargs):
        if not hasattr(Log, "_instance"):  # 反射
            Log._instance = object.__new__(cls)
        return Log._instance

    def __init__(self):
        super().__init__(name='root', level=logging.DEBUG)
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.INFO)
        self.time_at = time.strftime(
            '%Y%m%d_%H%M%S', time.localtime(time.time()))
        # file_name = os.path.join(os.path.dirname(__file__).split("C")[0],
        #                          f"Log/{time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time()))}.log")
        file_name = os.path.join(os.path.dirname(__file__).split("common")[0],
                                 f"Log/log.log")

        file_handler = logging.FileHandler(
            filename=file_name, encoding="utf-8")
        fmt = logging.Formatter(
            '%(asctime)s %(name)s  %(levelname)s  %(pathname)s - %(lineno)d line：%(message)s')
        file_handler.setFormatter(fmt=fmt)
        self.logger.addHandler(file_handler)


log = Log()
logger = log.logger
